package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WorkFormMapper;
import com.ruoyi.common.core.text.Convert;

/**
 * 工单表Service业务层处理
 *
 * @author ruoyi
 * @date 2020-04-20
 */
@Service
public class WorkFormServiceImpl implements IWorkFormService {
    @Autowired
    private WorkFormMapper workFormMapper;

    @Autowired
    private IWorkFaultService iWorkFaultService;

    @Autowired
    private IWorkPhotoService iWorkPhotoService;

    @Autowired
    private IWorkCustomerService iWorkCustomerService;
    @Autowired
    private ISysUserService iSysUserService;
    /**
     * 查询工单表
     *
     * @param id 工单表ID
     * @return 工单表
     */
    @Override
    public WorkForm selectWorkFormById(Long id) {
        return workFormMapper.selectWorkFormById(id);
    }

    /**
     * 查询工单表列表
     *
     * @param workForm 工单表
     * @return 工单表
     */
    @Override
    public List<WorkForm> selectWorkFormList(WorkForm workForm) {

        return workFormMapper.selectWorkFormList(workForm);
    }

    /**
     * 新增工单表
     *
     * @param workForm 工单表
     * @return 结果
     */
    @Override
    public int insertWorkForm(WorkForm workForm) {
        workForm.setCreateTime(DateUtils.getNowDate());
        return workFormMapper.insertWorkForm(workForm);
    }

    /**
     * 修改工单表
     *
     * @param workForm 工单表
     * @return 结果
     */
    @Override
    public int updateWorkForm(WorkForm workForm) {
        return workFormMapper.updateWorkForm(workForm);
    }

    /**
     * 删除工单表对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWorkFormByIds(String ids) {
        List<WorkFault> list = iWorkFaultService.selectWorkFaultByFormId(ids);
        for (WorkFault workFault : list) {
            Long id = workFault.getId();
            iWorkPhotoService.deleteWorkPhotoByFaultId(id);
            iWorkPhotoService.deleteWorkPhotoByFaultPhoto(id);
        }
        iWorkFaultService.deleteWorkFaultByFormId(ids);
        return workFormMapper.deleteWorkFormByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工单表信息
     *
     * @param id 工单表ID
     * @return 结果
     */
    @Override
    public int deleteWorkFormById(Long id) {
        return workFormMapper.deleteWorkFormById(id);
    }

    @Override
    public List<FaultCustForm> selectWorkFormListAll(WorkForm workForm,String name) {
        List<FaultCustForm> faultCustForms = selectAll(workForm,name,"0","0","0","0");
        return faultCustForms;
    }

    @Override
    public int insertWorkFormByIds(Long ids) {

        return workFormMapper.insertWorkFormByIds(ids);
    }

    @Override
    public int updataWorkFormById(Long id) {
        return workFormMapper.updataWorkFormById(id);
    }

    @Override
    public List<FaultCustForm> selectWorkCheckList(WorkForm workForm,String name) {

        List<FaultCustForm> faultCustForms = selectAll(workForm,name,"0","1","0","0");
        return faultCustForms;
    }

    @Override
    public int updateWorkFormBycheckOk(Long id,Long checkId) {
        WorkFormUser workFormUser = new WorkFormUser();
        workFormUser.setFormid(id+"");
        workFormUser.setFlag(checkId+"");
        List<WorkFormUser> list = workFormMapper.selectWorkFormUser(id+"");
        for (WorkFormUser formUser : list) {
            if(StringUtils.isEmpty(formUser.getFlag())){
                String flag = "2";
                int ids = formUser.getId().intValue();
                workFormMapper.updateWorkFormUserById(flag,ids);
            }
        }

        return workFormMapper.updateWorkFormBycheckOk(id,checkId);
    }

    @Override
    public List<FaultCustForm> selectWorkCheckListOk(WorkForm workForm,String name) {
        List<FaultCustForm> faultCustForms = selectAll(workForm,name,"0","1","1","0");
        List<FaultCustForm> faultCustForms1 = selectAll(workForm,name,"0","1","2","0");
        List<FaultCustForm> faultCustForms2 = selectAll(workForm,name,"1","1","1","0");
        List<FaultCustForm> faultCustForms3 = selectAll(workForm,name,"1","1","2","0");
        List<FaultCustForm> faultCustForms4 = selectAll(workForm,name,"1","2","2","0");
        List<FaultCustForm> faultCustForms5 = selectAll(workForm,name,"0","1","2","1");
        faultCustForms.addAll(faultCustForms1);
        faultCustForms.addAll(faultCustForms2);
        faultCustForms.addAll(faultCustForms3);
        faultCustForms.addAll(faultCustForms4);
        faultCustForms.addAll(faultCustForms5);
        return faultCustForms;
    }

    @Override
    public int updateWorkFormBycheckId(Long id) {
        return workFormMapper.updateWorkFormBycheckId(id);
    }

    @Override
    public List<FaultCustForm> selectWorkFinishCheckListOk(WorkForm workForm,String name) {
        List<FaultCustForm> faultCustForms = selectAll(workForm,name,"1","2","0","0");
        return faultCustForms;
    }

    @Override
    public List<FaultCustForm> selectWorkFinishFormListOk(WorkForm workForm,String name) {
        List<FaultCustForm> faultCustForms = selectAll(workForm,name,"1","2","1","0");
        return faultCustForms;
    }

    @Override
    public List<FaultCustForm> selectWorkFinishFormStatus(WorkForm workForm,String name) {
        List<FaultCustForm> faultCustForms = selectAll(workForm,name,"0","1","0","0");
        List<FaultCustForm> faultCustForms2 = selectAll(workForm,name,"1","2","0","0");
        List<FaultCustForm> faultCustForms3 = selectAll(workForm,name,"0","1","1","1");
        faultCustForms.addAll(faultCustForms2);
        faultCustForms.addAll(faultCustForms3);
        return faultCustForms;
    }
/*

 */
    @Override
    public int updateWorkFormByTransId(int userid, int FromId) {
        workFormMapper.updateWorkFormByTransId(FromId);
        WorkFormUser workFormUser = new WorkFormUser();
        workFormUser.setFormid(FromId+"");
        workFormUser.setUserid(userid+"");
        WorkForm workForm = workFormMapper.selectWorkFormById((long) FromId);
        workFormUser.setOldHold(workForm.getHoldUser());
        workFormMapper.updateWorkFormByTransIds(workFormUser);
        return 1;
    }

    @Override
    public int updateWorkFormByReturnId(Long id) {
        return workFormMapper.updateWorkFormByReturnId(id);
    }

    @Override
    public List<FaultCustForm> selectWorkTransToCheck(WorkForm workForm,String name) {
        List<FaultCustForm> faultCustForms = selectAll(workForm,name,"0","1","1","1");
        return faultCustForms;
    }

    @Override
    public int updateWorkFormBycheckff(Long id, Long returnId) {
        WorkFormUser workFormUser = new WorkFormUser();
        workFormUser.setFormid(id+"");
        workFormUser.setFlag(returnId+"");
        List<WorkFormUser> list = workFormMapper.selectWorkFormUser(id+"");
        int userId = 0;
        for (WorkFormUser formUser : list) {
            if(StringUtils.isEmpty(formUser.getFlag())){
                String flag = "1";
                int ids = formUser.getId().intValue();
                userId = Integer.parseInt(formUser.getUserid());
                workFormMapper.updateWorkFormUserById(flag,ids);
            }
        }
        int formd1 = id.intValue();
        return  workFormMapper.updateWorkFormBycheckff(formd1,userId);
    }

    @Override
    public Long insertWorkFormByForm(WorkForm workForm) {
        return workFormMapper.insertWorkFormByForm(workForm);
    }

    @Override
    public void updateWorkFormByWork(Long username,String complete) {
        workFormMapper.updateWorkFormByWork(username,complete);
    }

    @Override
    public int selectWorkCheckListByFormId(Long f) {
        return workFormMapper.selectWorkCheckListByFormId(f);
    }

    @Override
    public int deleteWorkFormByFormId(Long id) {
        WorkFault workFault = iWorkFaultService.selectWorkFaultByForm(id);
        Long id1 = workFault.getId();
        iWorkPhotoService.deleteWorkPhotoByFaultId(id1);
        iWorkPhotoService.deleteWorkPhotoByNothing(id1);
        workFormMapper.deleteWorkFormByFormId(id);
        workFormMapper.deleteWorkFormByForm(id);
        return 1;
    }

    public List<FaultCustForm> selectAll(WorkForm workForm,String loginName,String a,String b,String c,String d){
        List<WorkForm> list = workFormMapper.selectWorkFormList(workForm);
        List<WorkForm> formList = new ArrayList<>();
        for (WorkForm forms : list) {
            if ("admin".equals(loginName)){
                formList.add(forms);
            }else {
                if (forms.getHoldUser().equals(loginName)){
                    formList.add(forms);
                }
            }
        }
        List<FaultCustForm> faultCustForms = new ArrayList<>();
        if (!StringUtils.isEmpty(formList)) {
            for (WorkForm form : formList) {

                FaultCustForm faultCustForm = new FaultCustForm();
                faultCustForm.setId(form.getId());
                faultCustForm.setCreateUser(form.getCreateUser());
                faultCustForm.setHoldUser(form.getHoldUser());
                faultCustForm.setCreateTime(form.getCreateTime());
                String workStatus = form.getWorkStatus();
                faultCustForm.setWorkStatus(workStatus);
                faultCustForm.setReturnStatus(form.getReturnStatus());
                faultCustForm.setTransferStatus(form.getTransferStatus());
                faultCustForm.setCheckStatus(form.getCheckStatus());
                if("1".equals(form.getTransferStatus())){
                    String user = workFormMapper.selectWorkByTransId(form.getId());
                    faultCustForm.setTransToUser(user);
                }
                WorkFault workFault = iWorkFaultService.findByFormId(form.getId());
                WorkCustomer workCustomer = iWorkCustomerService.selectWorkCustomerById(workFault.getCustId());
                faultCustForm.setCustName(workCustomer.getName());
                if (StringUtils.isNotEmpty(workFault.getDescription())) {
                    faultCustForm.setFaultDescription(workFault.getDescription());
                }
                if(StringUtils.isNotEmpty(form.getCompleteDescription())){
                    faultCustForm.setCompleteDescription(form.getCompleteDescription());
                }
                List<WorkPhoto> list1 = iWorkPhotoService.selectWorkPhotoByFaultId(workFault.getId());
                if (StringUtils.isNotEmpty(list1)) {
                    ArrayList<String> list2 = new ArrayList<>();
                    ArrayList<Long> list3 = new ArrayList<>();
                    for (WorkPhoto workPhoto : list1) {
                        list2.add(workPhoto.getPhotoUrl());
                        list3.add(workPhoto.getId());
                    }
                    faultCustForm.setFaultPhotoAddress(list2);
                    faultCustForm.setFaultPhotoId(list3);
                }
                List<WorkPhoto> finishList = iWorkPhotoService.selectWorkPhotoByByFinishId(form.getId());
                if (StringUtils.isNotEmpty(finishList)){
                    ArrayList<String> finishlist2 = new ArrayList<>();
                    ArrayList<Long> finishlist3 = new ArrayList<>();
                    for (WorkPhoto workPhoto : finishList) {
                        finishlist2.add(workPhoto.getPhotoUrl());
                        finishlist3.add(workPhoto.getId());
                    }
                    faultCustForm.setFinishPhotoAddress(finishlist2);
                    faultCustForm.setFinishPhotoId(finishlist3);
                }
                if (a.equals(workStatus)&b.equals(form.getCheckStatus())&c.equals(form.getReturnStatus())&d.equals(form.getTransferStatus())){
                    faultCustForms.add(faultCustForm);
                }
            }
        }
        return faultCustForms;
    }
}
